import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';

class FlutterPickerTest extends StatefulWidget {
  @override
  _FlutterPickerTestState createState() => _FlutterPickerTestState();
}

class _FlutterPickerTestState extends State<FlutterPickerTest> {
  final List _pickerdata = [
    {
      "a": [
        {
          "a1": [1, 2, 3, 4, 5]
        },
        {
          "a2": [6, 7, 8, 9, 10]
        }
      ]
    },
    {
      "b": [
        {
          "b1": [11, 12, 13, 14, 15]
        },
        {
          "b2": [16, 17, 18, 19, 20]
        }
      ]
    }
  ];

  final List _commonData = [
    {
      'label': 'c',
      'children': [
        {
          'label': 'c1',
          'children': [
            {'label': '1', 'children': []},
            {'label': '2', 'children': []},
            {'label': '3', 'children': []},
            {'label': '4', 'children': []},
            {'label': '5', 'children': []},
          ]
        },
        {
          'label': 'c2',
          'children': [
            {'label': '6', 'children': []},
            {'label': '7', 'children': []},
            {'label': '8', 'children': []},
            {'label': '9', 'children': []},
            {'label': '10', 'children': []},
          ]
        }
      ]
    },
    {
      'label': 'd',
      'children': [
        {
          'label': 'd1',
          'children': [
            {'label': '11', 'children': []},
            {'label': '12', 'children': []},
            {'label': '13', 'children': []},
            {'label': '14', 'children': []},
            {'label': '15', 'children': []},
          ]
        },
        {
          'label': 'd2',
          'children': [
            {'label': '16', 'children': []},
            {'label': '17', 'children': []},
            {'label': '18', 'children': []},
            {'label': '19', 'children': []},
            {'label': '20', 'children': []},
          ]
        }
      ]
    }
  ];

  List<PickerItem<String>> _getPickerItemList(List list) {
    return list.map<PickerItem<String>>((e) {
      return PickerItem<String>(
        text: Text(e['label'] as String),
        value: e['label'] as String,
        children: _getPickerItemList(
            e['children'] is List ? e['children'] as List : []),
      );
    }).toList();
  }

  // 选择器
  void showPicker() {
    Picker picker = Picker(
      adapter: PickerDataAdapter<String>(
        pickerdata: _pickerdata,
      ),
      height: 250,
      selectionOverlay: CupertinoPickerDefaultSelectionOverlay(
        background: Colors.transparent,
      ),
      cancelTextStyle: TextStyle(color: Colors.blue),
      confirmTextStyle: TextStyle(color: Colors.blue),
      onConfirm: (Picker picker, List value) {
        print(value.toString());
        print(picker.getSelectedValues());
      },
    );

    picker.showModal(context);
  }

  // 弹框选择器
  void showPickerDialog() {
    Picker picker = Picker(
      adapter: PickerDataAdapter<String>(
        // pickerdata: _pickerdata,
        data: _getPickerItemList(_commonData),
      ),
      hideHeader: true,
      title: Text("弹框选择器"),
      cancelTextStyle: TextStyle(color: Colors.blue),
      confirmTextStyle: TextStyle(color: Colors.blue),
      onConfirm: (Picker picker, List value) {
        print(value.toString());
        print(picker.getSelectedValues());
      },
    );

    picker.showDialog(context);
  }

  // 数字选择器
  void showPickerNumber() {
    TextStyle fixTextStyle = TextStyle(
      fontSize: 15,
      fontWeight: FontWeight.w600,
    );

    Picker picker = Picker(
      adapter: NumberPickerAdapter(
        data: [
          NumberPickerColumn(
            begin: 0,
            end: 999,
            postfix: Text(
              '第',
              style: fixTextStyle,
            ),
            suffix: Text(
              '名',
              style: fixTextStyle,
            ),
          ),
          NumberPickerColumn(
            begin: 200,
            end: 100,
            jump: -10,
          ),
        ],
      ),
      delimiter: [
        PickerDelimiter(
          child: Container(
            width: 30.0,
            alignment: Alignment.center,
            child: Text('-'),
          ),
        ),
      ],
      selecteds: [1, 2],
      hideHeader: true,
      title: Text("数字选择器"),
      cancelTextStyle: TextStyle(color: Colors.blue),
      confirmTextStyle: TextStyle(color: Colors.blue),
      onConfirm: (Picker picker, List value) {
        print(value.toString());
        print(picker.getSelectedValues());
      },
    );

    picker.showDialog(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('多列选择器'),
      ),
      body: Container(
        padding: EdgeInsets.all(10.0),
        alignment: Alignment.topCenter,
        child: ListView(
          children: <Widget>[
            ElevatedButton(
              onPressed: () {
                showPicker();
              },
              child: Text('显示选择器'),
            ),
            TextButton(
              onPressed: () {
                showPickerDialog();
              },
              child: Text('弹框选择器'),
            ),
            ElevatedButton(
              onPressed: () {
                showPickerNumber();
              },
              child: Text('数字选择器'),
            ),
          ],
        ),
      ),
    );
  }
}
